/*requires加载js,text,elem*/
/**
 * @name requires
 * @param {URIString} url xx.js或css|xx.css或text|xx.tpl
 * @param {Boolean|Function} [asy]
 * @return {Promise}
 */
window.requires = function(url, asy) {
	if("string" != typeof url) return console.log('只接受单一地址'), !1;
	var $url = url.split('|');
	switch($url[0]) {
		case 'css':
			requires.css($url[1])
			break;
		case 'text':
			return requires.text($url[1], asy)
			break;
		default:
			return requires.js(url, asy)
	}
};

window.requiresObj = {};
window.requiresTextObj = {};

requires.js = function(url, asy) {
	var urlStr = url.replace(/http(s)?\:\/\//, '').replace(/\.|\:|\/|\-/g, '_');
	if(requiresObj[urlStr + '']) {
		return requiresObj[urlStr + '']
			.done(function() {
				($.type(asy) == 'function') && asy()
			})
			.fail(function() {
				console.error('文件' + url + '请求失败!')
			});
	} else {
		return requiresObj[urlStr + ''] = $.ajax({
				type: "get",
				url: url,
				async: asy ? true : false,
			})
			.done(function() {
				($.type(asy) == 'function') && asy()
			})
			.fail(function() {
				console.error('文件' + url + '请求失败!')
			});
	}
};

requires.css = function(url) {
	if(!document.querySelector('[sign="' + url + '"]')) {
		var dom = document.createElement("link");
		dom.setAttribute('href', url);
		dom.setAttribute('rel', 'stylesheet');
		dom.setAttribute('type', 'text/css');
		dom.setAttribute('sign', url);
		document.getElementsByTagName('head')[0].appendChild(dom);
	}
};

requires.text = function(url, fn) {
	var $href = url.split('#'),
		content = '',
		$hrefSrc = $href[0],
		$hrefID = $href[1] && $href[1];
	var urlStr = $hrefSrc.replace('http://', '').replace(/\.|\:|\/|\-/g, '_');

	var successFn = function(data) {
		if($hrefID) {
			if(requiresTextObj[urlStr + '_' + $hrefID]) {
				content = requiresTextObj[urlStr + '__' + $hrefID];
			} else {
				data = data.replace(/\<\!DOCTYPE/, '<div').replace(/<(html|head|body)/g, '<div').replace(/<\/(html|head|body)/g, '</div');
				var wrap = document.createElement("div");
				wrap.innerHTML = data;
				var dom = wrap.querySelector('#' + $hrefID);
				content = dom ? dom.outerHTML : '';
				requiresTextObj[urlStr + '__' + $hrefID] = content;
			}
		} else {
			content = data;
		}
	};

	if(fn) {
		if(requiresObj[urlStr + '']) {
			return requiresObj[urlStr + '']
				.done(function(data) {
					successFn(data);
					($.type(fn) == 'function') && fn(content);
				})
				.fail(function() {
					console.error('文件' + $hrefSrc + '请求失败')
				});
		} else {
			return requiresObj[urlStr + ''] = $.ajax({
					type: "get",
					url: $hrefSrc,
				})
				.done(function(data) {
					successFn(data);
					($.type(fn) == 'function') && fn(content);
				})
				.fail(function() {
					console.error('文件' + $hrefSrc + '请求失败')
				});
		}
	} else {
		if(requiresObj[urlStr + '']) {
			requiresObj[urlStr + '']
				.done(function(data) {
					successFn(data);
				})
				.fail(function() {
					console.error('文件' + $hrefSrc + '请求失败')
				});

			return content;
		} else {
			requiresObj[urlStr + ''] = $.ajax({
					type: "get",
					url: $hrefSrc,
					async: false
				})
				.done(function(data) {
					successFn(data);
				})
				.fail(function() {
					console.error('文件' + $hrefSrc + '请求失败')
				});

			return content;
		}
	}
};

window.imports = function() {
	var importsAll = document.querySelectorAll("imports:not([data-wl])");
	if(importsAll.length) {
		$.each(importsAll, function(i, _this) {
			_this.setAttribute('data-wl', '');
		});
		$.each(importsAll, function(i, _this) {
			var url = _this.getAttribute('href'),
				asy = _this.getAttribute('async');
			if(asy !== null) {
				requires('text|' + url, function(content) {
					var $content = $(content).addClass(_this.getAttribute('class') || '');
					$(_this).replaceWith($content);
				})
			} else {
				var $content = $(requires('text|' + url)).addClass(_this.getAttribute('class') || '');
				$(_this).replaceWith($content);
			}
		});
	};
};

$(function() {
	imports();
});